package com.snail.billing.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonWorkType;
import com.snail.billing.session.WorkTypeSession;

public class WorkTypeFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private ViewGroup layout;
	private List<Button> buttonList;
	private int position = -1;
	private List<LinearLayout> linearLayoutList;

	private LinearLayout childLinearLayout;
	private List<Button> childButtonList;

	private List<List<JsonWorkType.Type>> data;

	private WorkTypeSession workTypeSession;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_work_type_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText("我的提问");
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		layout = (ViewGroup) findViewById(ResUtil.getViewId(Resource.id.snailbilling_work_type_layout));
		layout.setVisibility(View.GONE);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (data == null) {
			requestWorkType();
		} else {
			setLayout();
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
			return;
		}
		for (int i = 0; i < buttonList.size(); i++) {
			if (v.equals(buttonList.get(i))) {
				if (i == position) {
					closeSelectButton();
				} else {
					setSelectButton(i);
				}
				return;
			}
		}
		for (int i = 0; i < childButtonList.size(); i++) {
			if (v.equals(childButtonList.get(i))) {
				JsonWorkType.Type type = data.get(position).get(i);
				WorkMoreFragment.workParams = new WorkMoreFragment.WorkParams();
				WorkMoreFragment.workParams.isNew = true;
				WorkMoreFragment.workParams.parentCode = type.getParentCode();
				WorkMoreFragment.workParams.code = type.getCode();
				getPageManager().forward(WorkMoreFragment.class);
				return;
			}
		}
	}

	private void setLayout() {
		//
		int num = 2;
		buttonList = new ArrayList<Button>();
		for (int index = 0; index < data.size(); index++) {
			Button button = new Button(getContext());
			button.setBackgroundResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_click_button_blue));
			button.setTextColor(0xffffffff);
			button.setText(data.get(index).get(0).getParentName());
			button.setOnClickListener(this);
			buttonList.add(button);
		}
		if (buttonList.size() % num > 0) {
			for (int i = 0; i < buttonList.size() % num; i++) {
				Button button = new Button(getContext());
				button.setBackgroundResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_click_button_blue));
				button.setTextColor(0xffffffff);
				button.setVisibility(View.INVISIBLE);
				buttonList.add(button);
			}
		}
		linearLayoutList = new ArrayList<LinearLayout>();
		for (int i = 0; i < buttonList.size() / num; i++) {
			LinearLayout linearLayout = new LinearLayout(getContext());
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.topMargin = 15;
			layoutParams.bottomMargin = 15;
			layout.addView(linearLayout, layoutParams);
			for (int j = 0; j < num; j++) {
				LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
				if (j != 0) {
					layoutParams2.leftMargin = 30;
				}
				linearLayout.addView(buttonList.get(num * i + j), layoutParams2);
			}
			linearLayoutList.add(linearLayout);
		}
		layout.setVisibility(View.VISIBLE);
	}

	private void setSelectButton(int position) {
		this.position = position;
		for (int i = 0; i < buttonList.size(); i++) {
			if (position == i) {
				buttonList.get(i).setBackgroundColor(0xff1f6494);
			} else {
				buttonList.get(i).setBackgroundResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_click_button_blue));
			}
		}
		int num = 2;
		childButtonList = new ArrayList<Button>();
		for (int index = 0; index < data.get(position).size(); index++) {
			Button button = new Button(getContext());
			button.setBackgroundColor(0x00ffffff);
			button.setPadding(0, 20, 0, 20);
			button.setTextColor(0xff000000);
			button.setTextSize(14.0f);
			button.setText(data.get(position).get(index).getName());
			button.setOnClickListener(this);
			childButtonList.add(button);
		}
		if (childButtonList.size() % num > 0) {
			for (int i = 0; i < childButtonList.size() % num; i++) {
				Button button = new Button(getContext());
				button.setBackgroundColor(0x00ffffff);
				button.setPadding(0, 20, 0, 20);
				button.setTextColor(0xff000000);
				button.setTextSize(14.0f);
				button.setVisibility(View.INVISIBLE);
				childButtonList.add(button);
			}
		}
		layout.removeView(childLinearLayout);
		childLinearLayout = new LinearLayout(getContext());
		childLinearLayout.setOrientation(LinearLayout.VERTICAL);
		childLinearLayout.setBackgroundColor(0xffffffff);
		List<LinearLayout> linearLayoutList = new ArrayList<LinearLayout>();
		for (int i = 0; i < childButtonList.size() / num; i++) {
			LinearLayout linearLayout = new LinearLayout(getContext());
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			childLinearLayout.addView(linearLayout, layoutParams);
			for (int j = 0; j < num; j++) {
				LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
				linearLayout.addView(childButtonList.get(num * i + j), layoutParams2);
			}
			linearLayoutList.add(linearLayout);
		}
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.topMargin = 5;
		layoutParams.bottomMargin = 5;
		layout.addView(childLinearLayout, position / 2 + 3, layoutParams);
	}

	private void closeSelectButton() {
		for (int i = 0; i < buttonList.size(); i++) {
			buttonList.get(i).setBackgroundResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_click_button_blue));
		}
		layout.removeView(childLinearLayout);
		position = -1;
	}

	private void requestWorkType() {
		HttpApp httpApp = new HttpApp(getContext());
		workTypeSession = new WorkTypeSession();
		httpApp.setOnHttpResultListener(this);
		httpApp.request(workTypeSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(workTypeSession)) {
				JsonWorkType jsonWorkType = new JsonWorkType((String) workTypeSession.getResponseData());
				if (jsonWorkType.getCode() == 1) {
					data = new ArrayList<List<JsonWorkType.Type>>();
					Map<String, Integer> map = new HashMap<String, Integer>();
					for (int index = 0; index < jsonWorkType.getTypes().size(); index++) {
						JsonWorkType.Type type = jsonWorkType.getTypes().get(index);
						if (!map.containsKey(type.getParentCode())) {
							map.put(type.getParentCode(), data.size());
							data.add(new ArrayList<JsonWorkType.Type>());
						}
						List<JsonWorkType.Type> list = data.get(map.get(type.getParentCode()));
						list.add(type);
					}
					setLayout();
				} else {
					Toast.makeText(getContext(), jsonWorkType.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}
}
